package com.lepu.app.fun.index.activity;

import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.ObserverConst;
import com.app.utils.Setting;
import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.DensityUtil;
import com.core.lib.utils.Observer;
import com.core.lib.utils.ObserverManager;
import com.core.lib.utils.main.UmengHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.chat.activity.DoctorMainActivity;
import com.lepu.app.fun.chat.bean.ChatSession;
import com.lepu.app.fun.chat.core.ChatManager;
import com.lepu.app.fun.chat.util.ChatConst;
import com.lepu.app.fun.chat.util.ChatUtil;
import com.lepu.app.usercenter.util.UserCenterUtil;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

import org.json.JSONException;
import org.json.JSONObject;

public class IndexConsultActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, Observer {

	private static final String REQUEST_PHONE = "request_phone";

	private TextView mOnlineChatCountTextView = null;
	private TextView mMyDoctorChatCountTextView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_consult_activity);

		init();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		UmengHelper.onResume(this, UmengEvent.page_shouye_kuaisuzixun);
		
		ObserverManager.getInstance().addObserver(
				ObserverConst.NOTIFY_CHAT_INDEX_ZIXUN_ONLINE_CHAT_CIRCLE, this);
		ObserverManager.getInstance().addObserver(
				ObserverConst.NOTIFY_CHAT_INDEX_ZIXUN_DOCTOR_CHAT_CIRCLE, this);
	
		showOnlineNewMsgCount();
		showDoctorNewMsgCount();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		
		UmengHelper.onPause(this, UmengEvent.page_shouye_kuaisuzixun);
		
		ObserverManager.getInstance().removeObserver(this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("快速咨询");
		topbar.setonTopbarNewLeftLayoutListener(this);

		//initOnlineUi();
		mOnlineChatCountTextView = (TextView) findViewById(R.id.onlineChatCountTextView);
		mMyDoctorChatCountTextView = (TextView) findViewById(R.id.myDoctorChatCountTextView);

		showOnlineNewMsgCount();
		showDoctorNewMsgCount();

		Button telButton = (Button) findViewById(R.id.telButton);
		telButton.setText(String.format("咨询电话：%s", Setting.TELL_PHONE));
	}
	
	@SuppressWarnings("unused")
	private void initOnlineUi()
	{
		LinearLayout onlineChatLayout = (LinearLayout) findViewById(R.id.onlineChatLayout);
	
		int windowWidth = UtilityBase.getWindowWidthHeight(this)[0];
		int leftRightMargin = DensityUtil.dip2px(this, 12);
		int middleMargin = DensityUtil.dip2px(this, 25);
		
		int viewWidthHeight = (windowWidth - 2 * leftRightMargin - 3 * middleMargin) / 4;

		LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		int[] resArray = new int[]{R.drawable.index_nurse_icon_1,R.drawable.index_nurse_icon_2,R.drawable.index_nurse_icon_3,R.drawable.index_nurse_icon_4};
		for (int i = 0; i < resArray.length; i++) {

			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
			imageView.setBackgroundResource(resArray[i]);
			imageView.getLayoutParams().width = viewWidthHeight;
			imageView.getLayoutParams().height = viewWidthHeight;
			
			if (i != 0) {
				params.setMargins(middleMargin, 0, 0, 0);
				params.gravity = Gravity.CENTER;
			}
			imageView.setLayoutParams(params);

			onlineChatLayout.addView(imageView);
		}
	}

	public void viewClick(View view) {
		int rid = view.getId();
		switch (rid) {
		case R.id.onlineChatLayout:
		case R.id.onlineNewCountTextView:

			boolean flagOnlineChat = LoginUtil.jumpLogin(this);
			if (flagOnlineChat) {
				return;
			}

			if(rid == R.id.onlineChatLayout)
			{
				UmengHelper.CustomEvent(this, UmengEvent.event_kuaisuzixun_tongxinyisheng_yishengtouxiang);
			}else{
				UmengHelper.CustomEvent(this, UmengEvent.event_kuaisuzixun_tongxinyisheng_xinxiaoxi);
			}
			
			// 聊天界面
			ChatUtil.jumpChat(this, null, ChatConst.TYPE_ONLINE_CHAT_ONLINE);
			break;
		case R.id.telButton:
			
			UmengHelper.CustomEvent(this, UmengEvent.event_kuaisuzixun_zixundianhu);
			
			tell(Setting.TELL_PHONE);
			break;
		case R.id.myDoctorChatLayout:
		case R.id.doctorNewCountTextView:

			boolean flagMyDoctor = LoginUtil.jumpLogin(this);
			if (flagMyDoctor) {
				return;
			}
			
			if(rid == R.id.myDoctorChatLayout)
			{
				UmengHelper.CustomEvent(this, UmengEvent.event_kuaisuzixun_zhuzhiyisheng_wodeyisheng);
			}else{
				UmengHelper.CustomEvent(this, UmengEvent.event_kuaisuzixun_zhuzhiyisheng_xinxiaoxi);
			}
			
			Intent doctorMain = new Intent(this, DoctorMainActivity.class);
			startActivity(doctorMain, true);
			break;

		default:
			break;
		}
	}

	private void tell(final String phone) {

		if(TextUtils.isEmpty(phone)){
			return;
		}

		MyDialog myDialog = new MyDialog(this);
		myDialog.setTitle("拨打电话").setMessage(phone).
				setNegativeButton(R.string.cancel, null).
				setPositiveButton(R.string.app_ok, new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Uri uri = Uri.parse("tel:" + phone);
						Intent intentCallPhone = new Intent(Intent.ACTION_DIAL, uri);
						startActivity(intentCallPhone);

					}
				}).create(null).show();
	}

	private void showOnlineNewMsgCount() {
//		ChatSession chatSession = ChatManager.getInstance()
//				.getChatStaffSession(Constants.Staff);
//		if (chatSession != null) {
//			mOnlineChatCountTextView.setVisibility(View.VISIBLE);
//			mOnlineChatCountTextView.setText(String.valueOf(chatSession
//					.getNewMsgCount()));
//		} else {
//			mOnlineChatCountTextView.setVisibility(View.GONE);
//		}
		
		int newCount = LoginUtil.getStaffMsgNewCount();
		if(newCount > 0)
		{
			mOnlineChatCountTextView.setVisibility(View.VISIBLE);

			if(newCount > 999){
				mOnlineChatCountTextView.setText("999");
			}else{
				mOnlineChatCountTextView.setText(String.valueOf(newCount));
			}
		}else{
			mOnlineChatCountTextView.setVisibility(View.GONE);
		}
	}

	private void showDoctorNewMsgCount() {
		List<ChatSession> sessions = ChatManager.getInstance().getChatSessions();
		if(sessions != null && sessions.size() > 0)
		{
			int newCount = 0;
			for(ChatSession item : sessions)
			{
				newCount += item.getNewMsgCount();
			}

			if(newCount > 0)
			{
				mMyDoctorChatCountTextView.setVisibility(View.VISIBLE);
				mMyDoctorChatCountTextView.setText(String.valueOf(newCount));

			}else{
				mMyDoctorChatCountTextView.setVisibility(View.GONE);
			}
		}else{
			mMyDoctorChatCountTextView.setVisibility(View.GONE);
		}
	}

	/**
	 * main thread
	 */
	@Override
	public void notify(String name, Object sender, Object data) {

		if (name.equals(ObserverConst.NOTIFY_CHAT_INDEX_ZIXUN_ONLINE_CHAT_CIRCLE)) {
			// 在线医生红点
			showOnlineNewMsgCount();
		} else if (name
				.equals(ObserverConst.NOTIFY_CHAT_INDEX_ZIXUN_DOCTOR_CHAT_CIRCLE)) {
			// 主治医生红点
			showDoctorNewMsgCount();
		}
	}
}
